#!/bin/bash

# url, output md5

if [ -f /etc/debian_version ]; then
	export OS_TYPE=Debian
elif [ -f /etc/redhat-release ]; then
	export OS_TYPE=Centos
else
	echo "无法识别OS类型\n"
	exit 0;
fi

export OS_ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
export OS_VERSION=$(awk '/DISTRIB_RELEASE=/' /etc/*-release | sed 's/DISTRIB_RELEASE=//' | sed 's/[.]0/./')

if [ -z "$OS_VERSION" ]; then
    OS_VERSION=$(awk '{print $3}' /etc/*-release)
fi

export OPENSSL_VERSION=1.1.1l
export OPENSSL_MD5SUM=ac0d4387f3ba0ad741b0580dd45f6ff3

export THREAD_NUM=8

export PHP7_BASE_PATH=
export PHP8_BASE_PATH=


if [ -f /usr/local/php/bin/php ]; then
	PHP_VERSION=$(/usr/local/php/bin/php -v | grep "^PHP [0-9]" | cut -c 5-7)
	case ${PHP_VERSION} in
    "7.0"|"7.1"|"7.2"|"7.3"|"7.4")
		export PHP7_BASE_PATH=/usr/local/php
		export PHP8_BASE_PATH=/usr/local/php8
		;;
	"8.0"|"8.1"|"8.2")
		export PHP8_BASE_PATH=/usr/local/php
		export PHP7_BASE_PATH=/usr/local/php7
		;;
	esac
fi

if [ -f /usr/local/php8/bin/php ]; then
	PHP_VERSION=$(/usr/local/php8/bin/php -v | grep "^PHP [0-9]" | cut -c 5-7)
	export PHP7_BASE_PATH=/usr/local/php
	export PHP8_BASE_PATH=/usr/local/php8
fi

if [ -f /usr/local/php7/bin/php ]; then
	PHP_VERSION=$(/usr/local/php7/bin/php -v | grep "^PHP [0-9]" | cut -c 5-7)
	export PHP7_BASE_PATH=/usr/local/php7
	export PHP8_BASE_PATH=/usr/local/php
fi


mkdir -p output

showmenu()
{
	cat << EOF
`echo -e "\033[35mOS: ${OS_TYPE}${OS_VERSION}-${OS_ARCH}\033[0m"`
`echo -e "\033[35m----------------------------------------------------\033[0m"`
`echo -e "\033[35m|*************** 请选择要部署的软件 ***************|\033[0m"`
`echo -e "\033[35m----------------------------------------------------\033[0m"`
`echo -e "\033[35m|          1) 部署WEB服务器(tengine)               |\033[0m"`
`echo -e "\033[35m|          2) 部署PHP8执行环境                     |\033[0m"`
`echo -e "\033[35m|          3) 部署Swoole插件                       |\033[0m"`
`echo -e "\033[35m|          x) 退出                                 |\033[0m"`
`echo -e "\033[35m----------------------------------------------------\033[0m"`
EOF
}

showmenu
while read -p "请输入对应产品的数字："  num
do
	case $num in
		1)
			echo "开始安装tengine";
			pushd output;
			../scripts/tengine.sh;
			popd;
		;;
		2)
			echo "开始安装PHP8";
			pushd output;
			../scripts/php8.sh;
			popd;
		;;
		3)
			echo "开始安装swoole";
			pushd output;
			../scripts/swoole.sh;
			popd;
		;;
		x)
			exit 0;
		;;
		*)
			echo "错误的选项: $num 请重新输入".;
		;;
	esac
	showmenu
done


